package com.xingkeduo.config;


import com.rabbitmq.client.ConnectionFactory;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;


@Configuration
public class RabbitMQConfig {

    @Resource
    private RabbitTemplate rabbitTemplate;

    @PostConstruct
    public void initMq(){
        rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
    }

    public static final String IMMEDIATE_QUEUE_XDELAY="immediate_queue_xdelay";
    public static final String DELAYED_EXCHANGE_XDELAY="delayed_exchange_xdelay";
    public static final String DELAY_ROUTING_KEY_XDELAY="delay_routing_key_xdelay";


    /**延迟队列配置**/
/*

    @Bean
    public Queue close_order(){
        Map<String, Object> params = new HashMap<>();
        params.put("x-dead-letter-exchange","order.delay.exchange");
        params.put("x-dead-letter-routing-key","all");
        return new Queue("TaskModelEnum.close_order", true,false,false,params);
    }

    @Bean
    public Queue close_suixingfu_order(){
        Map<String, Object> params = new HashMap<>();
        params.put("x-dead-letter-exchange","order.delay.exchange");
        params.put("x-dead-letter-routing-key","all");
        return new Queue("TaskModelEnum.close_suixingfu_order", true,false,false,params);
    }

    @Bean
    public Queue test_order(){
        Map<String, Object> params = new HashMap<>();
        params.put("x-dead-letter-exchange","order.delay.exchange");
        params.put("x-dead-letter-routing-key","all");
        return new Queue("TaskModelEnum.test", true,false,false,params);
    }


    @Bean
    public TopicExchange registerDelayExchange(){
        return new TopicExchange("order.register.delay.exchange");
    }

    @Bean
    public Binding close_orderregisterDelayBinding(){
        return BindingBuilder.bind(close_order()).to(registerDelayExchange()).with("close_order");
    }

    @Bean
    public Binding close_suixingfu_orderDelayBinding(){
        return BindingBuilder.bind(close_suixingfu_order()).to(registerDelayExchange()).with("close_suixingfu_order");
    }
    @Bean
    public Binding testregisterDelayBinding(){
        return BindingBuilder.bind(test_order()).to(registerDelayExchange()).with("test");
    }
*/


    /**延迟队列配置**/

    /**指标消费队列配置**/

    @Bean
    public Queue registerQueue(){
        return new Queue("order.delay.queue",true);
    }

    @Bean
    public TopicExchange registerTopicExchange(){
        return new TopicExchange("order.delay.exchange");
    }

    @Bean
    public Binding registerBinding(){
        return BindingBuilder.bind(registerQueue()).to(registerTopicExchange()).with("all");
    }



    @Bean
    public Queue immediateQueue() {
        // 第一个参数是创建的queue的名字，第二个参数是是否支持持久化
        return new Queue(IMMEDIATE_QUEUE_XDELAY, true);
    }

    @Bean
    public CustomExchange delayExchange() {
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("x-delayed-type", "direct");
        return new CustomExchange(DELAYED_EXCHANGE_XDELAY, "x-delayed-message", true, false, args);
    }

    @Bean
    public Binding bindingNotify() {
        return BindingBuilder.bind(immediateQueue()).to(delayExchange()).with(DELAY_ROUTING_KEY_XDELAY).noargs();
    }



}
